//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CDirectoryData.h>
///	@path	~/src/database/xdata/
///	@date	2007/11/26
///	@desc	Local file directory entry.

#pragma once

#include "xeres.h"

#include "lib/utilities/dict.h"
#include "lib/system/lock.h"

#include "database/IData.h"
#include "database/store/IFile.h"
#include "database/store/IFileSystem.h"

namespace xeres {

	/*!
		\class	CDirectoryData
		\brief	
	*/
	class CDirectoryData : public IData
	{
	public:

		/// \ctor
		CDirectoryData( RefWeak<IFileSystem> filesystem , const WString& path , const WString& as_path );

		/// \dtor
		virtual ~CDirectoryData( void );

		DECL_IOBJECT_CLASS( CDirectoryData , IData );

	public:

	//@ interface

		/*!
			\brief		Get the internal data type name.
			\return		Type name.
		*/
		virtual const WString& GetVarTypeName( void ) const;

		/*!
			\brief		Get internal data type id.
			\return		Type ID.
		*/
		virtual const ClassID GetVarTypeID( void ) const;

		/*!
			\brief		Get data info.
		*/
		virtual bool GetInfo( DataInfo& datainfo );

		/*!
			\brief		Get data entry key.
		*/
		virtual const Key& GetKey( void ) const;

		/*!
			\brief		Create a new entry.
		*/
		virtual RefPass<IData> NewChild( const Key& key );

		/*!
			\brief		Get child entry count.
		*/
		virtual size_t GetChildCount( void );

		/*!
			\brief		Get child entry by key.
		*/
		virtual RefPass<IData> GetChild( const Key& key );

		/*!
			\brief		Get child entry by index.
		*/
		virtual RefPass<IData> GetChild( size_t index );

		/*!
			\brief		Get child info by key.
		*/
		virtual bool GetChildInfo( const Key& key , DataInfo& info );

		/*!
			\brief		Get child info by index.
		*/
		virtual bool GetChildInfo( size_t index , DataInfo& info , WString& name );

		/*!
			\brief		Find child by key, based on the index to start, return index and result.
		*/
		virtual bool FindChildIndex( const Key& key , size_t& index );

		/*!
			\brief		Find child by key, and based on index.
		*/
		virtual RefPass<IData> FindChild( const Key& key , size_t& index );

		/*!
			\brief		Save data entry to storage.
		*/
		virtual bool Save( void );

		/*!
			\brief		Reload data entry from storage.
		*/
		virtual bool Reload( void );

		/*! 
			\brief		Open file.
		*/
		virtual RefPass<IFile> OpenFile( const WString& file );

		/*! 
			\brief		Open IStream.
		*/
		virtual struct IStream * OpenIStream( const WString& file );

	private:

		// ask for file
		RefPass<IData> AskForFile( const Key& file , bool create_new );

		// cache call back
		static void CacheDirCallback( void * data , const IFileSystem::IEnumFileData& iefd );


	//@ data

		// parent file system
		Ref< IFileSystem >			m_filesystem;

		// key of current directory
		Key							m_key;

		// path related to file system
		WString						m_path;

		// last modify
		DateTime					m_modified;

		// files in directory
		std::vector< Key >			m_files;

		// sub directory cache
		std::vector< Ref< CDirectoryData > >	m_dirs;
	};

} // namespace xeres
